package weaver.formmode.customjavacode.modeexpand.holyview;


import com.engine.crm.util.CrmRemindUtil;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.formmode.customjavacode.modeexpand.holyview.utils.HolyviewUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.outinterface.HrmOutInterface;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 * @Description:
 * @Param:
 * @return:
 * @Author: Dusongsong
 * @Date: 2021/3/23
 */
public class WeaNewAgentModeExpand extends AbstractModeExpandJavaCodeNew {

    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            User user = (User) param.get( "user" );
            int userid = user.getUID();
            int billid = -1;
            int modeid = -1;
            RequestInfo requestInfo = (RequestInfo) param.get( "RequestInfo" );
            if (requestInfo != null) {
                billid = Util.getIntValue( requestInfo.getRequestid() );
                modeid = Util.getIntValue( requestInfo.getWorkflowid() );
                if (billid > 0 && modeid > 0) {
                    /*
                     *@Description:
                     */
                    RecordSet rs = new RecordSet();
                    rs.writeLog( "WeaNewAgentModeExpand>>>>>billid=" + billid );
                    String curdate = DateUtil.getCurrentDate();
                    rs.executeQuery( "select dlsmc,dlsbh,dlszh,dlsmm,zt from uf_dlszhcj where id=? and (dlszh is not null or dlszh !='')", billid );
                    if (rs.next()) {
                        String dlsmc = Util.null2String( rs.getString( "dlsmc" ) );
                        String dlsbh = Util.null2String( rs.getString( "dlsbh" ) );
                        String dlszh = Util.null2String( rs.getString( "dlszh" ) );
                        String dlsmm = Util.null2String( rs.getString( "dlsmm" ) );
                        int zt = Util.getIntValue( rs.getString( "zt" ));
                        String hrmid = HolyviewUtil.hasRepeatByHrm( dlszh );
                        rs.writeLog( "WeaNewAgentModeExpand>>>>>hrmid=" + hrmid );
                        HrmOutInterface hrmOutInterface = new HrmOutInterface();
                        CrmRemindUtil crmRemindUtil = new CrmRemindUtil(user);
                        String lastname = Util.null2String(dlsmc);
                        String loginid = Util.null2String(dlszh);
                        String mobile = Util.null2String("");
                        String email = Util.null2String("");
                        String seclevel = Util.null2String("-10");
                        String password = Util.getEncrypt(dlsmm);
                        String password2 = Util.null2String(password);
                        String isoutmanager = Util.null2String("0");
                        String wxname = Util.null2String(dlsbh);
                        String wxopenid = Util.null2String("");
                        String wxuuid = Util.null2String(UUID.randomUUID());
                        String country = Util.null2String("");
                        String province = Util.null2String("");
                        String city = Util.null2String("");
                        String customid = Util.null2String("116");
                        String customfrom = Util.null2String("");
                        Map<String, String> params = new HashMap<String, String>();
                        if (zt == 0) {
                        if ("".equals( hrmid ) ){
                            params.put("crmid", customid);
                            params.put("lastname", lastname);
                            params.put("loginid", loginid);
                            params.put("password", password);
                            params.put("seclevel", seclevel);
                            params.put("mobile", mobile);
                            params.put("email", email);
                            params.put("isoutmanager", isoutmanager);
                            params.put("wxname", wxname);
                            params.put("wxopenid", wxopenid);
                            params.put("wxuuid", wxuuid);
                            params.put("country", country);
                            params.put("province", province);
                            params.put("city", city);
                            params.put("customfrom", customfrom);
                            Map<String, String> returnObj = hrmOutInterface.createResource4Card(params);
                            String errorinfo = returnObj.get("errorinfo");
                            rs.writeLog("errorinfo="+errorinfo);
                            //外部用户ID
                            String resourceid = returnObj.get("resourceid");
                            String sql = "INSERT INTO CRM_ShareInfo (relateditemid, sharetype, seclevel, sharelevel, crmid, contents, deleted, seclevelMax, jobtitleid, joblevel, scopeid) VALUES (?, '1', '10','1','0',?, '0', '100', ?, '0', ',0,')";
                            rs.executeUpdate(sql, customid, resourceid, resourceid);
                            crmRemindUtil.outResourceAddRemind(Integer.parseInt(resourceid));
                        } else {
                            String sql = "";
                            sql = " select a.lastname,a.loginid,c.departmentid,a.password,a.mobile,a.email,a.seclevel,b.wxname," +
                                    "	b.wxopenid,b.wxuuid,b.country,b.province,b.city,b.customid,b.customfrom,isoutmanager " +
                                    " from HrmResource a, hrmresourceout b, HrmResourceVirtual c " +
                                    " WHERE a.id=b.resourceid AND a.id=c.resourceid and a.id = " + Util.getIntValue(hrmid, -1);
                            rs.executeQuery(sql);
                            rs.first();
                            String rndFlag = password2;
                            if (!rndFlag.equals("aaaaaa") && !rndFlag.equals("")) {
                                password = Util.getEncrypt(password);
                            } else {
                                password = rs.getString("password");
                            }
                            if ("undefined".equals(customid) || "".equals(customid)) {
                                customid = rs.getString("customid");
                            }

                            params.put("crmid", customid);
                            params.put("lastname", lastname);
                            params.put("loginid", loginid);
                            params.put("password", password);
                            params.put("seclevel", seclevel);
                            params.put("mobile", mobile);
                            params.put("email", email);
                            params.put("isoutmanager", isoutmanager);
                            params.put("wxname", wxname);
                            params.put("wxopenid", wxopenid);
                            params.put("wxuuid", wxuuid);
                            params.put("country", country);
                            params.put("province", province);
                            params.put("city", city);
                            params.put("customfrom", customfrom);
                            hrmOutInterface.updateResourceById4Card(params);
                            crmRemindUtil.outResourceEditRemind(Integer.parseInt(hrmid));
                        }
                    } else {
                            String[] arr = hrmid.split(",");
                            RecordSet recordSet = new RecordSet();
                            String name = "";
                            String delOutNames = "";
                            crmRemindUtil.outResourceBatchDeleteRemind(hrmid);
                            if (!arr.equals("")) {
                                for (int i = 0; i < arr.length; i++) {
                                    recordSet.executeQuery("SELECT h.lastname from hrmresourceout o join hrmresource h on o.resourceid = h.id where o.resourceid =? ", arr[i]);
                                    if (recordSet.next()) {
                                        name = recordSet.getString("lastname");
                                        delOutNames = delOutNames + name + ",";
                                    }
                                    hrmOutInterface.delete(arr[i]);
                                    String sql = "delete  from CRM_ShareInfo where contents=?";
                                    rs.executeUpdate(sql, hrmid);
                                }
                                crmRemindUtil.outResourceBatchDeleteRemind(hrmid);
                            }

                        }
                    }
                }
            }
        } catch (Exception e) {
            result.put( "errmsg", "The interface returns the information error" );
            result.put( "flag", "false" );
        }
        return result;
    }
}